package com.webproject.shop.service.impl;

import com.webproject.shop.dao.ProductMapper;
import com.webproject.shop.pojo.Product;
import com.webproject.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product findProductById(int id){
        //查商品
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    @Transactional
    public boolean decreaseAmount(Integer id,Integer amount) {
        //减库存
        int rows = productMapper.decreaseAmount(id,amount);
        return rows > 0;
    }

    @Override
    public List<Product> findAll() {
        //全部商品列表
        List<Product> products = productMapper.selectAll();
        return products;
    }

    @Override
    public List<Product> selectByClass(String proClass) {
        //按分类检索
        List<Product> products = productMapper.selectByClass(proClass);
        return products;
    }

    @Override
    public List<Product> selectByName(String name) {
        //按大致名称搜索
        List<Product> products = productMapper.selectByName(name);
        return products;
    }


}
